﻿import GameModuleC from "./GameModuleC";
import GameModuleData from "./GameModuleData";

export default class GameModuleS extends ModuleS<GameModuleC, GameModuleData> {

    /**房间内的玩家列表 */
    private playerList: Player[] = [];

    /**先手玩家 */
    private firstPlayer: Player = null;
    /**后手玩家 */
    private secondPlayer: Player = null;

    /** 当脚本被实例后，会在第一帧更新前调用此函数 */
    protected onStart(): void {
        console.log("角色服务端模块启动")
    }

    protected onPlayerEnterGame(player: mw.Player): void {
        this.playerList.push(player);
        if (this.playerList.length == 2) {
            this.gameStart();
        }
    }

    protected onPlayerLeft(player: mw.Player): void {
        this.playerList.splice(this.playerList.findIndex(_player => _player == player), 1);
    }


    private gameStart() {
        let firstIndex = Math.floor(Math.random() * 2);
        this.firstPlayer = this.playerList[firstIndex];
        this.secondPlayer = this.playerList[(firstIndex + 1) % this.playerList.length];

        console.log("先手玩家为" + this.firstPlayer.character.displayName);
        this.getClient(this.firstPlayer).net_onGameStart(true);
        this.getClient(this.secondPlayer).net_onGameStart(false);
        console.log();

       // 在客户端延迟3秒后执行坐椅子的操作
       setTimeout(() => {
        this.playerSitOnChair(this.firstPlayer, "3F4A786C");
        this.playerSitOnChair(this.secondPlayer, "2C8F71F0");
    }, 3000);
    }

    // 玩家坐在椅子上
    private async playerSitOnChair(player: Player, chairId: string): Promise<void> {
        // 获取交互物
        const chair = await GameObject.asyncFindGameObjectById(chairId) as Interactor;
        // 设置椅子的交互
        chair.slot = HumanoidSlotType.Buttocks;
        chair.animationId = "4175";
        // 玩家坐在椅子上
        chair.enter(player.character, HumanoidSlotType.Buttocks, "4175");
    }
}